//
//  PBSearchHomeController.m
//  PBHomeDemo
//
//  Created by Playboy on 2017/11/22.
//  Copyright © 2017年 tmkj. All rights reserved.
//

#import "PBSearchHomeController.h"
#import "MenuListView.h"
#import "PBHomeCell.h"
#import "PBBestHomeModel.h"
#import "PBHomeDetailController.h"
#import "TMSearchEmptyView.h"

static NSString * const kPBHomeCell = @"PBHomeCell";

@interface PBSearchHomeController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property (nonatomic, strong) MenuListView      *menu;
@property (strong, nonatomic) IBOutlet UITextField *searchTextField;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UIView *searchView;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *topConstraint;
@property (nonatomic, strong) NSMutableArray   *dataSource;
@property (nonatomic, strong) TMSearchEmptyView *searchEmptyView;
@end

@implementation PBSearchHomeController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self configure];
}

- (void)configure {
    [self configureView];
    [self configerChooseList];
    [self configureData];
}

- (void)configureView {
    self.searchView.layer.cornerRadius = 21;
    self.searchView.layer.masksToBounds = YES;
    self.searchTextField.delegate = self;
    [self.tableView registerNib:[UINib nibWithNibName:kPBHomeCell bundle:nil] forCellReuseIdentifier:kPBHomeCell];
    self.topConstraint.constant = Height_StatusBar;
}
- (void)configureData {
    NSDictionary *param = @{@"cityId":[TMShareData shareData].cityId};
    [TMNetwork requestPostWithParam:param url:@"/house/excellent/query" callback:^(TMBaseReqModel *model) {
        [self.tableView.mj_header endRefreshing];
        if (model.code == 200) {
            self.dataSource = [PBBestHomeModel objectArrayWithKeyValuesArray:model.data];
            [self.tableView reloadData];
        }else {
            [self showHint:model.message];
        }
    }];
}

- (void)configerChooseList {
    UIImage *defImg = [UIImage imageNamed:@"scenic_hot_arrow_down"];
    UIImage *selImg = [UIImage imageNamed:@"scenic_hot_arrow_up"];
    NSInteger frameY;
    NSInteger frameEmptyY;
    if (IS_IPHONE_X) {
        frameY = 64+22;
        frameEmptyY = 64+40+22;
    }else {
        frameY = 64;
        frameEmptyY = 64+40;
    }
    
    CGRect frame = CGRectMake(0.f, frameY, kScreenWidth, 40.f);
    
    MenuListView *menu = [[MenuListView alloc] initWithFrame:frame Titles:@[@"合租/整租",@"位置",@"入住如期",@"筛选"] defImage:defImg selImage:selImg];
    self.menu = menu;
    __weak typeof (menu)weakMenu = menu;
    menu.clickMenuButton = ^(MenuButton *button, NSInteger index, BOOL selected){
        if (index == 0) {
            weakMenu.dataSource = @[@"合租",@"整租",@"直租"];
        }else if (index == 1) {
            weakMenu.dataSource = @[@"附近",@"地铁"];
        }else if (index == 2) {
            weakMenu.dataSource = @[@"今天",@"明天",@"后天"];
        }else if (index == 3) {
            weakMenu.dataSource = @[@"推荐排序",@"热门周边"];
        }
    };
    
    // 选中下拉列表某行时的回调（这个回调方法请务必实现！）
//    menu.clickListView = ^(NSInteger index, NSString *title, NSInteger i){
//        NSLog(@"%@",title);
//    };
    
    menu.clickTheValue = ^(NSDictionary *valus) {
        NSLog(@"%@",valus);
        [self showHud];
        [TMNetwork requestPostWithParam:valus url:@"/house/query" callback:^(TMBaseReqModel *model) {
            [self hideHud];
            if (model.code == 200) {
                [self.searchEmptyView removeFromSuperview];
                self.dataSource = [PBBestHomeModel objectArrayWithKeyValuesArray:model.data];
                if (self.dataSource.count == 0) {
                    self.searchEmptyView = [TMSearchEmptyView TMSearchEmptyView];
                    self.searchEmptyView.frame = CGRectMake(0, frameEmptyY, kScreenWidth, self.tableView.mj_h);
                    [self.view addSubview:self.searchEmptyView];
                }
                [self.tableView reloadData];
            }else {
                [self showHint:model.message];
            }
        }];
    };
    
    [self.view addSubview:menu];
}

#pragma mark - <UITableViewDelegate,UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PBHomeCell *cell = [tableView dequeueReusableCellWithIdentifier:kPBHomeCell forIndexPath:indexPath];
    [cell refreshWithModel:self.dataSource[indexPath.row]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PBBestHomeModel *model = self.dataSource[indexPath.row];
    PBHomeDetailController *vc = [[PBHomeDetailController alloc] init];
    vc.houseId = [NSString stringWithFormat:@"%zd",model.ID];
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self.view endEditing:YES];
    [self.menu hideListViewAnimation];
    NSDictionary *param = @{@"keyword":textField.text};
    [self showHud];
    [TMNetwork requestPostWithParam:param url:@"/house/query" callback:^(TMBaseReqModel *model) {
        [self hideHud];
        if (model.code == 200) {
            self.dataSource = [PBBestHomeModel objectArrayWithKeyValuesArray:model.data];
            if (self.dataSource.count == 0) {
                self.searchEmptyView = [TMSearchEmptyView TMSearchEmptyView];
                self.searchEmptyView.frame = CGRectMake(0, Height_NavBar, kScreenWidth, self.tableView.mj_h);
                [self.view addSubview:self.searchEmptyView];
            }else {
                [self.searchEmptyView removeFromSuperview];
            }
            [self.tableView reloadData];
        }else {
            [self showHint:model.message];
        }
    }];
    return YES;
}

- (IBAction)backButtonClick:(id)sender {
    [self.menu hideListViewAnimation];
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - super
- (void)resetNavigationBar {
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
